package com.linlang.grasharepro.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


import com.linlang.grasharepro.model.Experience;
import com.linlang.grasharepro.utils.DataBaseOpenHelper;

import java.util.ArrayList;

/**
 * 经验名片表
 * Created by lenovo on 2016/8/3.
 */
public class ExperienceDB {
    public static final String SQLITE_TABLE = "experienceCard";

    public static final String PK = "_id";
    public static final String GRADUATION_TIME = "graduationTime";
    public static final String COMPANY = "company";
    public static final String POSITION = "position";
    public static final String CONTENT = "content";
    public static final String TREATMENT = "treatment";
    public static final String EXPERINCE = "experince";
    public static final String DIFFERENT = "different";
    public static final String IS_RECRUIT = "isRecruit";
    public static final String RELATION = "relation";
    public static final String RELATION_WAY = "relationWay";

    private static final String TAG = "collectionDbAdapter";
    private DatabaseHelper databaseHelper;
    private SQLiteDatabase sqLiteDatabase;
    private final Context context;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context context) {

            super(context, DataBaseOpenHelper.DATABASE_NAME, null, DataBaseOpenHelper.DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
            onCreate(db);
        }
    }

    public ExperienceDB(Context context) {
        this.context = context;
    }

    public ExperienceDB open() throws SQLException {
        databaseHelper = new DatabaseHelper(context);
        sqLiteDatabase = databaseHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        if (databaseHelper != null) {
            databaseHelper.close();
        }
    }

    /**
     * 新增
     *
     * @param experience
     * @return
     */
    public int insertExperience(Experience experience) {
        long createResult = 0;
        int strid = 0;
        ContentValues initialValues = new ContentValues();
        initialValues.put(GRADUATION_TIME, experience.getGraduationTime());
        initialValues.put(COMPANY, experience.getCompany());
        initialValues.put(POSITION, experience.getPosition());
        initialValues.put(CONTENT, experience.getContent());
        initialValues.put(TREATMENT, experience.getTreatment());
        initialValues.put(EXPERINCE, experience.getExperince());
        initialValues.put(DIFFERENT, experience.getDifferent());
        initialValues.put(IS_RECRUIT, experience.getIsRecruit());
        initialValues.put(RELATION, experience.getRelation());
        initialValues.put(RELATION_WAY, experience.getRelationWay());
        try {
            createResult = sqLiteDatabase.insert(SQLITE_TABLE, null, initialValues);

            Cursor cursor = sqLiteDatabase.rawQuery("select last_insert_rowid() from " + SQLITE_TABLE, null);
            if (cursor.moveToFirst())
                strid = cursor.getInt(0);

        } catch (Exception e) {
            // TODO: handle exception
        }
        return strid;
    }

    /**
     * 删除表的全部字段数据
     *
     * @return
     */
    public boolean deleteAllExperience() {
        int doneDelete = 0;
        try {
            doneDelete = sqLiteDatabase.delete(SQLITE_TABLE, null, null);
            Log.w(TAG, Integer.toString(doneDelete));
            Log.e("doneDelete", doneDelete + "");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return doneDelete > 0;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    public boolean deleteById(String id) {
        int isDelete;
        String[] ids;
        ids = new String[]{id};
        isDelete = sqLiteDatabase.delete(SQLITE_TABLE, PK + "=?", ids);
        Log.e("deleteTicket", "isDelete:" + isDelete + "||" + "ticketID="
                + id);
        return isDelete > 0;
    }

    /**
     * 获取表中的所有字段
     *
     * @return
     */
    public ArrayList<Experience> fetchAll() {

        ArrayList<Experience> experiences = new ArrayList<Experience>();
        Cursor mCursor = null;
        mCursor = sqLiteDatabase.query(SQLITE_TABLE, new String[]{PK, GRADUATION_TIME, COMPANY,
                POSITION, CONTENT, TREATMENT, EXPERINCE, DIFFERENT, IS_RECRUIT, RELATION, RELATION_WAY}, null, null, null, null, null);
        if (mCursor.moveToFirst()) {
            do {
                Experience experience = new Experience();
                experience.set_id(PK);
                experience.setGraduationTime(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(GRADUATION_TIME)));
                experience.setCompany(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(COMPANY)));
                experience.setPosition(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(POSITION)));
                experience.setContent(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(CONTENT)));
                experience.setTreatment(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(TREATMENT)));
                experience.setExperince(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(EXPERINCE)));
                experience.setDifferent(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(DIFFERENT)));
                experience.setIsRecruit(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(IS_RECRUIT)));
                experience.setRelation(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(RELATION)));
                experience.setRelationWay(mCursor.getString(mCursor
                        .getColumnIndexOrThrow(RELATION_WAY)));
                experiences.add(experience);
            } while (mCursor.moveToNext());
        }
        if (mCursor != null && !mCursor.isClosed()) {
            mCursor.close();
        }
        return experiences;
    }

    public Experience queryById(String id) {
        Experience experience = new Experience();
        Cursor mCursor = null;
        mCursor = sqLiteDatabase.query(SQLITE_TABLE, null, PK + "=?", new String[]{id}, null, null, null);
        if (mCursor.moveToFirst()) {
            experience.set_id(PK);
            experience.setGraduationTime(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(GRADUATION_TIME)));
            experience.setCompany(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(COMPANY)));
            experience.setPosition(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(POSITION)));
            experience.setContent(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(CONTENT)));
            experience.setTreatment(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(TREATMENT)));
            experience.setExperince(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(EXPERINCE)));
            experience.setDifferent(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(DIFFERENT)));
            experience.setIsRecruit(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(IS_RECRUIT)));
            experience.setRelation(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(RELATION)));
            experience.setRelationWay(mCursor.getString(mCursor
                    .getColumnIndexOrThrow(RELATION_WAY)));
        }
        if (mCursor != null && !mCursor.isClosed()) {
            mCursor.close();
        }
        return experience;
    }
}
